﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using UwaySoft.EsheetServer.BizLogic;

namespace UwaySoft.EsheetServer.Help
{
    public class UserCityHelper
    {
        private static List<int> _cities = new List<int>
        { 
            20, 757, 662, 663, 668, 660, 751, 752, 753, 754, 755, 756, 758, 759, 750,
            762, 763, 766, 768, 769, 760
        };

        private static XmlDocument _xmlDoc;

        public UserCityHelper()
        {
            _xmlDoc = XmlHelper.OpenAsXmlDocument(ConfigHelper.UserCityConfigPath);
        }

        public int GetCityId(string cityNo)
        {
            if (string.IsNullOrEmpty(cityNo))
                return -1;

            if (_xmlDoc == null)
                return -1;

            XmlNodeList nodes = _xmlDoc.SelectNodes("/UserCitys/City");

            if (nodes.Count == 0)
                return -1;

            int cityId = -1;
            foreach (XmlNode node in nodes)
                if (node.Attributes["No"].InnerText == cityNo)
                {
                    int id;
                    if (int.TryParse(node.Attributes["Id"].InnerText, out id))
                        cityId = id;
                    break;
                }

            if (cityId != -1 && _cities.FindAll(c => c == cityId).Count == 0)
            {
                return -1;
            }

            return cityId;
        }
    }
}
